package com.mall.module.product.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.enums.CommonStatusEnum;
import com.mall.common.result.Result;
import com.mall.module.product.domain.dto.BatchOperationDTO;
import com.mall.module.product.domain.dto.ProductCategoryDTO;
import com.mall.module.product.domain.vo.ProductCategoryVO;
import com.mall.module.product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 商品分类 Controller
 */
@RestController
@RequestMapping("/api/product/category")
@RequiredArgsConstructor
@Api(tags = "商品分类管理", value = "提供商品分类的增删改查及树形结构查询")
@Slf4j
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;

    /**
     * 分页查询商品分类
     */
    @ApiOperation(value = "分页查询商品分类", notes = "支持按分类名称和状态筛选")
    @GetMapping
    public Result<PageInfo<ProductCategoryVO>> queryByPage(
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,

        @ApiParam(value = "每页数量", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize,

        @ApiParam(value = "分类名称(模糊查询)")
        @RequestParam(required = false) String name,

        @ApiParam(value = "状态(0:禁用 1:启用)", allowableValues = "0,1")
        @RequestParam(required = false) Integer status) {

        PageInfo<ProductCategoryVO> pageInfo = productCategoryService.page(pageNum, pageSize, name, status);
        return Result.success(pageInfo);
    }

    /**
     * 获取分类树形结构
     */

    @ApiOperation(value = "获取分类树形结构", notes = "根据父级ID查询子分类树")
    @GetMapping("/tree")
    public Result<List<ProductCategoryVO>> queryCategoryTree(
        @ApiParam(value = "父级分类ID(空时查询根分类)")
        @RequestParam(required = false) Long parentId) {

        List<ProductCategoryVO> tree = productCategoryService.tree(parentId);
        return Result.success(tree);
    }


    /**
     * 根据ID获取详情
     */
    @ApiOperation(value = "获取分类详情", notes = "根据ID查询分类详细信息")
    @GetMapping("/{id}")
    public Result<ProductCategoryVO> getDetail(
        @ApiParam(value = "分类ID", required = true)
        @PathVariable @NotNull Long id) {
        ProductCategoryVO detail = productCategoryService.getDetail(id);
        return Result.success(detail);
    }

    /**
     * 新增商品分类
     */
    @ApiOperation(value = "创建商品分类", notes = "新建商品分类")
    @PostMapping
    public Result<Long> createCategory(
        @ApiParam(value = "分类信息", required = true)
        @RequestBody @Valid ProductCategoryDTO dto) {

        log.info("创建商品分类: {}", dto.getName());
        Long id = productCategoryService.add(dto);
        return Result.success(id, "创建成功");
    }

    /**
     * 修改商品分类
     */
    @ApiOperation(value = "更新商品分类", notes = "根据ID更新分类信息")
    @PutMapping("/{id}")
    public Result<Void> updateCategory(
        @ApiParam(value = "分类ID", required = true)
        @PathVariable @NotNull Long id,

        @ApiParam(value = "更新数据", required = true)
        @RequestBody @Valid ProductCategoryDTO dto) {

        log.info("更新商品分类: ID={}", id);
        dto.setId(id);
        productCategoryService.update(dto);
        return Result.successMsg("更新成功");
    }


    /**
     * 删除商品分类
     */

    @ApiOperation(value = "删除商品分类", notes = "根据ID删除单个分类")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(
        @ApiParam(value = "分类ID", required = true)
        @PathVariable @NotNull Long id) {

        log.warn("删除商品分类: ID={}", id);
        productCategoryService.delete(id);
        return Result.successMsg("删除成功");
    }

//    /**
//     * 批量删除商品分类
//     */
//    @ApiOperation(value = "批量删除分类", notes = "根据ID列表批量删除分类")
//    @DeleteMapping
//    public Result<Void> batchDeleteCategories(
//        @ApiParam(value = "分类ID列表", required = true)
//        @RequestBody @Valid BatchOperationDTO batchIds) {
//
//        log.warn("批量删除分类: IDs={}", batchIds.getIds());
//        productCategoryService.batchDelete(batchIds.getIds());
//        return Result.successMsg("批量删除成功");
//    }
//
//    /**
//     * 批量修改状态
//     */
//    @ApiOperation(value = "批量更新状态", notes = "批量启用/禁用分类")
//    @PatchMapping("/status")
//    public Result<Void> batchUpdateStatus(
//        @ApiParam(value = "目标状态", required = true, allowableValues = "0,1")
//        @RequestParam @NotNull Integer status,
//        @ApiParam(value = "分类ID列表", required = true)
//        @RequestBody @Valid BatchOperationDTO batchIds) {
//        CommonStatusEnum statusEnum = CommonStatusEnum.of(status);
//        log.info("批量更新状态: {} -> {}", statusEnum.getDesc(), batchIds.getIds());
//        productCategoryService.batchUpdateStatus(batchIds.getIds(), status);
//        return Result.successMsg("状态更新成功");
//    }


}